<?php
/**
 * Implementation of PDO
 *
 * @package Rapide_Database
 * @author Adam Brodziak adam.brodziak@rapide.pl
 * @since 0.4dev rev 83
 * @link http://www.php.net/pdo
 */
class Rapide_Database_PDO extends Rapide_Database
{

	protected $_oPdo;

	public function connect()
	{
		if (!is_object($this->_oPdo))
		{
			$sDsn = $this->_makeDsn();
			$this->_oPdo = new PDO($sDsn, $this->_aParameters['user'], $this->_aParameters['password'], array(PDO::ATTR_PERSISTENT => $this->_aParameters['persistent']));
		}
	}

	public function disconnect()
	{
	}

	/**
	 * Creates a Data Source Name string
	 *
	 * Includes only classic DNS with driver, databasename and host. Others DSN aren't supported (yet).
	 *
	 * @return string DSN
	 */
	private function _makeDsn()
	{
		if (!isset($this->_aParameters['driver']))
		{
			$this->_aParameters['driver'] = 'mysql';
		}
		return $this->_aParameters['driver'] . ':dbname=' . $this->_aParameters['database'] . ';host=' . $this->_aParameters['host'];
	}

	public function query($sQuery)
	{
		parent::query($sQuery);
		return $this->_oPdo->query($sQuery);
	}

	public function exec($sQuery)
	{
		parent::exec($sQuery);
		return $this->_oPdo->exec($sQuery);
	}

	public function lastInsertId($sName = null)
	{
		if (is_null($sName))
		{
			return $this->_oPdo->lastInsertId();
		}
		else
		{
			return $this->_oPdo->lastInsertId($sName);
		}
	}

	public function prepare($sqlStatement, array $aDriverOptions = array())
	{
		array_push($this->_aQueryStack, $sqlStatement);
		if (!empty($aDriverOptions))
		{
			return $this->_oPdo->prepare($sqlStatement, $aDriverOptions);
		}
		else
		{
			return $this->_oPdo->prepare($sqlStatement);
		}
	}

	public function quote($sName)
	{
		return $this->_oPdo->quote($sName);
	}

	public function errorCode()
	{
		return $this->_oPdo->errorCode();
	}

	public function errorInfo()
	{
		return $this->_oPdo->errorInfo();
	}

	public function getAttribute($iAttribute)
	{
		return $this->_oPdo->getAttribute($iAttribute);
	}

	public function setAttribute($iAttribute, $mValue)
	{
		return $this->_oPdo->setAttribute($iAttribute, $mValue);
	}

	public function beginTransaction()
	{
		return $this->_oPdo->beginTransaction();
	}

	public function commit()
	{
		return $this->_oPdo->commit();
	}

	public function rollBack()
	{
		return $this->_oPdo->rollBack();
	}


}
?>
